﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.PQ
{
    public partial class ht_pq_add : PageBase
    {
        public string whe = "", path = "", tab = "pq_ht";
        public string us, um, ubh;          
        protected void Page_Load(object sender, EventArgs e)
        {

            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();

            if (!IsPostBack)
            {
                //if (!String.IsNullOrEmpty(Request["stype"]))
                //{
                //     hidstype.Text = Request["stype"].Trim();
                
                //}
                fill();
                Regcomp();
            }
            else
            {
                fillcomp();
            }

        
        }

        void Regcomp()
        {
            ExtAspNet.PageContext.RegisterStartupScript
                           (
                           "var d=Ext.getCmp('" + ddlcompany.ClientID + "');" +
                           " var oldInput=\"\";" +
                           " d.listWidth=500; " +
                           " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                           " var newInput=document.getElementById(\"" + ddlcompany.ClientID + "\").value;" +
                            " if (oldInput==newInput) {return;} " +
                           " oldInput=newInput;" +
                            " __doPostBack('loadcomp',newInput);" +
                            "});"
                           );

            ExtAspNet.PageContext.RegisterStartupScript
                 (
                 "var d=Ext.getCmp('" + ddlperson.ClientID + "');" +
                 " var oldInput=\"\";" +
                 " d.listWidth=500; " +
                 " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                 " var newInput=document.getElementById(\"" + ddlperson.ClientID + "\").value;" +
                  " if (oldInput==newInput) {return;} " +
                 " oldInput=newInput;" +
                  " __doPostBack('loadpepo',newInput);" +
                  "});"
                 );


            ExtAspNet.PageContext.RegisterStartupScript
                (
                "var d=Ext.getCmp('" + ddlcomp.ClientID + "');" +
                " var oldInput=\"\";" +
                " d.listWidth=500; " +
                " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                " var newInput=document.getElementById(\"" + ddlcomp.ClientID + "\").value;" +
                 " if (oldInput==newInput) {return;} " +
                " oldInput=newInput;" +
                 " __doPostBack('loadcp',newInput);" +
                 "});"
                );

            ExtAspNet.PageContext.RegisterStartupScript
                  (
                  "var d=Ext.getCmp('" + ddlschool.ClientID + "');" +
                  " var oldInput=\"\";" +
                  " d.listWidth=500; " +
                  " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                  " var newInput=document.getElementById(\"" + ddlschool.ClientID + "\").value;" +
                   " if (oldInput==newInput) {return;} " +
                  " oldInput=newInput;" +
                   " __doPostBack('loadsc',newInput);" +
                   "});"
                  );

            ExtAspNet.PageContext.RegisterStartupScript
             (
             "var d=Ext.getCmp('" + ddlzj.ClientID + "');" +
             " var oldInput=\"\";" +
             " d.listWidth=500; " +
             " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
             " var newInput=document.getElementById(\"" + ddlzj.ClientID + "\").value;" +
              " if (oldInput==newInput) {return;} " +
             " oldInput=newInput;" +
              " __doPostBack('loadzj',newInput);" +
              "});"
             );

            ExtAspNet.PageContext.RegisterStartupScript
              (
              "var d=Ext.getCmp('" + ddlsales.ClientID + "');" +
              " var oldInput=\"\";" +
              " d.listWidth=500; " +
              " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
              " var newInput=document.getElementById(\"" + ddlsales.ClientID + "\").value;" +
               " if (oldInput==newInput) {return;} " +
              " oldInput=newInput;" +
               " __doPostBack('loadss',newInput);" +
               "});"
              );

            ExtAspNet.PageContext.RegisterStartupScript
              (
              "var d=Ext.getCmp('" + ddlzsales.ClientID + "');" +
              " var oldInput=\"\";" +
              " d.listWidth=500; " +
              " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
              " var newInput=document.getElementById(\"" + ddlzsales.ClientID + "\").value;" +
               " if (oldInput==newInput) {return;} " +
              " oldInput=newInput;" +
               " __doPostBack('loadzss',newInput);" +
               "});"
              );

        }

        void fillcomp()
        {

            if (Request["__EVENTTARGET"] == "loadcomp")
            {

                string sqltxt = "SELECT * FROM company where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',cname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlcompany.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlcompany.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["cname"].ToString(), dt.Rows[i]["cname"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlcompany.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }


            if (Request["__EVENTTARGET"] == "loadpepo")
            {
                string sqltxt = "SELECT * FROM pq_member where charindex('" +
                                 Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlperson.Items.Clear();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlperson.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlperson.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

            if (Request["__EVENTTARGET"] == "loadcp")
            {
                string sqltxt = "SELECT * FROM company where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',cname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlcomp.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlcomp.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["cname"].ToString(), dt.Rows[i]["cname"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlcomp.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

            if (Request["__EVENTTARGET"] == "loadsc")
            {
                string sqltxt = "SELECT * FROM school where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',schname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlschool.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlschool.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["schname"].ToString(), dt.Rows[i]["schbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlschool.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

            if (Request["__EVENTTARGET"] == "loadzj")
            {
                string sqltxt = "SELECT * FROM zj where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',zname)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlzj.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlzj.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["zname"].ToString(), dt.Rows[i]["zbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlzj.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }


            if (Request["__EVENTTARGET"] == "loadss")
            {
                string sqltxt = "SELECT * FROM sys_user where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlsales.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlsales.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlsales.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }

            if (Request["__EVENTTARGET"] == "loadzss")
            {
                string sqltxt = "SELECT * FROM sys_user where charindex('" +
                    Request["__EVENTARGUMENT"].ToString() + "',user_name)>0 ";
                // txtnm.Text = Request["__EVENTARGUMENT"].ToString();

                DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
                ddlzsales.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlzsales.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["user_name"].ToString(), dt.Rows[i]["sbh"].ToString()));
                }


                //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlzsales.ClientID + "\").value='" +
                         Request["__EVENTARGUMENT"] + "';");

            }


        }


        //填 充ddl
        void fill()
        {
           
            //公司
            DataTable e = BLL.Bllbase.get_table("[company]");
            ddlcompany.Items.Clear();
            ddlcomp.Items.Clear();

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcompany.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));
                ddlcomp.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["cname"].ToString().Trim(), e.Rows[i]["cbh"].ToString().Trim()));
            }

            ddlcompany.DataBind();
            ddlcomp.DataBind();

            ddlcompany.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlcomp.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlcompany.SelectedIndex = ddlcompany.Items.IndexOf(ddlcompany.Items.FindByText(""));
            ddlcomp.SelectedIndex = ddlcomp.Items.IndexOf(ddlcomp.Items.FindByText(""));

            //派遣员工
            e = BLL.Bllbase.get_table("[pq_member]");
            ddlperson.Items.Clear();        

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlperson.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
            }
            ddlperson.DataBind();          
            ddlperson.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByText(""));
          


            //员工
            e = BLL.Bllbase.get_table("[sys_user]");
            ddlsales.Items.Clear();
            ddlzsales.Items.Clear();

            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzsales.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));

                ddlsales.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["user_name"].ToString().Trim(), e.Rows[i]["sbh"].ToString().Trim()));
         
            }
            ddlzsales.DataBind();
            ddlsales.DataBind();

            ddlsales.Items.Add(new ExtAspNet.ListItem("",""));
            ddlzsales.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlzsales.SelectedIndex = ddlzsales.Items.IndexOf(ddlzsales.Items.FindByText(""));
            ddlsales.SelectedIndex = ddlsales.Items.IndexOf(ddlsales.Items.FindByText(""));


            //校园
             e = BLL.Bllbase.get_table("[school]");
             ddlschool.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlschool.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["schname"].ToString().Trim(), e.Rows[i]["schbh"].ToString()));
            }
            ddlschool.DataBind();
            ddlschool.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlschool.SelectedIndex = ddlschool.Items.IndexOf(ddlschool.Items.FindByText(""));

            //中介
            e = BLL.Bllbase.get_table("[zj]");
            ddlzj.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlzj.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["zname"].ToString().Trim(), e.Rows[i]["zbh"].ToString()));
            }
            ddlzj.DataBind();
            ddlzj.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlzj.SelectedIndex = ddlzj.Items.IndexOf(ddlzj.Items.FindByText(""));
       
        }
      
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {             
            add_member();

            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(hidstype.Text) + ActiveWindow.GetHidePostBackReference());
        }

        void add_member()
        {
            Model.PQ.pq_ht s = new Model.PQ.pq_ht();

            s.htbh = txtht.Text;

            s.pbh = ddlperson.SelectedValue;
            s.pname = ddlperson.SelectedText;
            s.pvendor = ddlcompany.SelectedText;
            s.pvbh = ddlcompany.SelectedValue;
            s.httype = "company";
         
            if (VAN.IsDateTime(date1.Text))
                s.sdate = DateTime.Parse(date1.Text);
            else
                s.sdate = DateTime.Parse("1900-01-01");

            if (VAN.IsDateTime(date2.Text))
                s.edate = DateTime.Parse(date2.Text);
            else
                s.edate = DateTime.Parse("1900-01-01");

            s.ftype = "";
            s.fbh = "";
            s.fname = "";

            
            s.owen = "";
            if (ddlcomp.SelectedValue != "")
            {
                s.ftype = "企业";
                s.fbh=ddlcomp.SelectedValue;
                s.fname = ddlcomp.SelectedText;
            }

            if (ddlschool.SelectedValue != "")
            {
                s.ftype = "学校";
                s.fbh = ddlschool.SelectedValue;
                s.fname = ddlschool.SelectedText;
            }


            if (ddlzj.SelectedValue != "")
            {
                s.ftype = "中介";
                s.fbh = ddlzj.SelectedValue;
                s.fname = ddlzj.SelectedText;
            }

            if (ddlsales.SelectedValue != "")
            {
                // s.ftype = "业务员";
                // s.fbh = ddlsales.SelectedValue;
                // s.fname = ddlsales.SelectedText;
                s.owen = ddlsales.SelectedText;
            }
            else
                s.owen = "";

            s.zsales = "";
            s.zsales = ddlzsales.SelectedText;
            

            s.money = 0;
            s.money =decimal.Parse(txtmoney.Text);

            s.work = "";
            s.work  =txtworktype.Text;

            s.status = 0;

            s.memo = "";
            s.memo = txtmemo.Text;

            BLL.PQ.pq_ht t = new BLL.PQ.pq_ht();
            t.Add(s);

            //更新业务员操作记录
            //.Bllbase.update_rec(tab, um, " htbh='" + s.htbh + "' ");
            string sqltxt = "update  pq_ht set  updtime=getdate(), sales='" +um+ "',sbh='"+ubh+"' where 1=1 and htbh='" + s.htbh + "'";
            DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);

        }
       
    }
}